package com.example.shopbusiness.controller;

import com.example.shopbusiness.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("api/order")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping(value = "/purchase")
    public String purchase(Long userId,Long pid,Boolean rollback, Integer count) {
        int orderCount = 10;
        if (count != null) {
            orderCount = count;
        }
        if(rollback==null){
            rollback = false;
        }
        try {
            businessService.purchase(userId,pid, orderCount, rollback);
        } catch (Exception exx) {
            return "Purchase Failed:" + exx.getMessage();
        }
        return "SUCCESS";
    }

}
